<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Repository;

use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
use Hyperf\Database\Model\Builder;
use Plugin\Attractions\AttractionManagement\Model\AttractionTags;

class AttractionTagsRepository extends IRepository
{
    public function __construct(protected readonly AttractionTags $model) {}

    public function handleSearch(Builder $query, array $params): Builder
    {
        return $query
            ->when(Arr::get($params, 'name'), static function (Builder $query, $name) {
                $query->where('name', 'like', '%' . $name . '%');
            });
    }

    public function list(array $params = []): Collection
    {
        return $this->perQuery($this->getQuery(), $params)
            ->get(['id', 'name']);
    }

    public function showListPage(array $params): array
    {
        $page = intval(Arr::get($params, 'page', 1));
        $pageSize = intval(Arr::get($params, 'pageSize', 20));
        return $this->perQuery($this->getQuery(), $params)
            ->simplePaginate($pageSize, ['id', 'name'], 'page', $page)
            ->items();
    }
}